package com.slj.comm.responce;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.util.List;

/**
 * @author CC
 * @version 1.0
 * @description: 分页结果集
 * @date 2025/9/11 8:24
 */
@Data
public class PageResult<T>{
    private Long total;       // 总记录数
    private List<T> list;     // 当前页数据
    private Integer pageNum;  // 当前页码
    private Integer pageSize; // 每页条数
    private Integer pages;    // 总页数
    private Boolean hasNext;  // 是否有下一页
    private Boolean hasPrev;  // 是否有上一页

    // 可选：添加构造方法
    private PageResult() {
    }

    private PageResult(Long total, List<T> list, Integer pageNum, Integer pageSize) {
        this.total = total;
        this.list = list;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.pages = (int) Math.ceil((double) total / pageSize);
        this.hasNext = pageNum < this.pages && this.pages > 0;
        this.hasPrev = pageNum > 1;
    }

    // 静态工厂方法（推荐）
    public static <T> PageResult<T> of(IPage page) {
        return new PageResult<>(
                page.getTotal(),
                page.getRecords(),
                (int) page.getCurrent(),
                (int) page.getSize()

        );
    }
}
